package Handler;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.*;
import Modelo.*;

@WebServlet("/Combustible_Vehiculo_Handler")
public class Combustible_Vehiculo_Handler extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	private static String ADD = "/Combustible_VehiculoAdd.jsp";
    private static String Edit = "/Combustible_Vehiculoedit.jsp";
    private static String Combustible_VehiculoRecord="/ListCombustible_Vehiculo.jsp";
    private Combustible_VehiculoDao dao;
    
    public Combustible_Vehiculo_Handler() {
        super();
        dao = new Combustible_VehiculoDao();
    }

    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";       
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("editform")){         
		        redirect = Edit;            
		}
		else if (action.equalsIgnoreCase("addnew")){         
		        redirect = ADD;            
		}else if (action.equalsIgnoreCase("listCombustible_Vehiculo")) {
			redirect = Combustible_VehiculoRecord;
			request.setAttribute("Combustible", dao.getAllCombus_Veh());
		} 
		
		 RequestDispatcher rd = request.getRequestDispatcher(redirect);
		 rd.forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";
        String combId = request.getParameter("combustibleVehiculoId");        
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("insert")) {
			Combustible_Vehiculo objCombusVeh = new Combustible_Vehiculo();
			objCombusVeh.setCombustibleId(Integer.parseInt(request.getParameter("CombustibleId")));
			objCombusVeh.setVehiculoId(Integer.parseInt(request.getParameter("VehiculoId")));
			objCombusVeh.setCapacidad_Max(Float.parseFloat(request.getParameter("Capacidad_Max")));
			objCombusVeh.setDescripcion(request.getParameter("Descripcion"));
			dao.addCombus_Veh(objCombusVeh);
			redirect = Combustible_VehiculoRecord;
			request.setAttribute("Combustible_Vehiculo", dao.getAllCombus_Veh());
			System.out.println("Record Added Successfully");
		}

		else if (action.equalsIgnoreCase("delete")) {
			String combveh_Id = request.getParameter("combustibleVehiculoId");
			int combidveh = Integer.parseInt(combveh_Id);
			dao.removeCombus_Veh(combidveh);
			redirect = Combustible_VehiculoRecord;
			request.setAttribute("Combustible_Vehiculo", dao.getAllCombus_Veh());
			System.out.println("Record Deleted Successfully");

		} else if (action.equalsIgnoreCase("edit")) {
			String combveh_Id = request.getParameter("combustibleVehiculoId");
			int combidveh = Integer.parseInt(combveh_Id);
			Combustible_Vehiculo objCombusVeh = new Combustible_Vehiculo();
			objCombusVeh.setCombustibleVehiculoId(combidveh);
			objCombusVeh.setCombustibleId(Integer.parseInt(request.getParameter("CombustibleId")));
			objCombusVeh.setVehiculoId(Integer.parseInt(request.getParameter("VehiculoId")));
			objCombusVeh.setCapacidad_Max(Float.parseFloat(request.getParameter("Capacidad_Max")));
			objCombusVeh.setDescripcion(request.getParameter("Descripcion"));
			dao.editCombus_Vehiculo(objCombusVeh);
			request.setAttribute("Combustible_Vehiculo", objCombusVeh);
			redirect = Combustible_VehiculoRecord;
			System.out.println("Record updated Successfully");

		} else if (action.equalsIgnoreCase("listCombustible_Vehiculo")) {
			redirect = Combustible_VehiculoRecord;
			request.setAttribute("Combustible_Vehiculo", dao.getAllCombus_Veh());
		} else {
			redirect = ADD;
		}

    RequestDispatcher rd = request.getRequestDispatcher(redirect);
    rd.forward(request, response);
	}

}
